/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DocumentSource;

import cn.com.chinasoa.course.subscribe.model.ArrangedCourse;
import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;
import cn.com.chinasoa.restkernel.transreptor.AbstractDocumentTransreptor;
/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-7-30下午02:18:19
 */
public class CourseInfo extends AbstractDocumentTransreptor {

	public void processDocument(Document source, Result tResult, Map tParamerters) {
		Document result = DocumentHelper.createDocument();
		Element resultRoot = result.addElement("table");
		Object courseIdObj = tParamerters.get("courseId");
		if((courseIdObj!=null)&&(courseIdObj instanceof String)){
			String courseId = (String)courseIdObj;
			long courseIdLong = DataTypeUtil.parseLong(courseId);
			if(courseIdLong>=0){
				EntityManager em = EntityManagerHelper.getEntityManager();
				ArrangedCourse vArrangedCourse= em.find(ArrangedCourse.class, courseIdLong);
				if(vArrangedCourse!=null){
					resultRoot.addElement("tr").addElement("th").addAttribute("style","font-size: 13").setText("课程信息");
					Element tr1 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr1.addElement("td").addElement("strong").setText("课程名称:");
					tr1.addElement("td").addElement("a").addAttribute("title", vArrangedCourse.getTrainingSubject().getSummary()).addAttribute("target","_blank").addAttribute("href",vArrangedCourse.getCourseDescription()).addElement("strong").setText(vArrangedCourse.getTrainingSubject().getCode());

					Element tr2 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr2.addElement("td").addElement("strong").setText("课程介绍:");
					tr2.addElement("td").addElement("a").addAttribute("title", vArrangedCourse.getTrainingSubject().getDescription()).addAttribute("target","_blank").addAttribute("href",vArrangedCourse.getCourseDescription()).addElement("strong").setText(vArrangedCourse.getCourseDescription());
					
					Element tr3 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr3.addElement("td").addElement("strong").setText("授课时间:");
					tr3.addElement("td").setText(DataTypeUtil.renderCalenderCN(vArrangedCourse.getStartDate())+"-"+DataTypeUtil.renderCalenderCN(vArrangedCourse.getEndDate()));
					
					Element tr4 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr4.addElement("td").addElement("strong").setText("授课地点:");
					tr4.addElement("td").addElement("a").addAttribute("title", vArrangedCourse.getTrainingAddress().getTrafficInfo()).addAttribute("href","#").addElement("strong").setText(vArrangedCourse.getTrainingAddress().getAddress());
					
					Element tr5 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr5.addElement("td").addElement("strong").setText("培训费用:");
					tr5.addElement("td").setText(vArrangedCourse.getTrainingSubject().getPrice()+"元/人");
					
					Element tr6 = resultRoot.addElement("tr").addElement("td").addElement("table").addElement("tr");
					tr6.addElement("td").addElement("strong").setText("优惠措施:");
					List<PrivilegeStrategy> psList = vArrangedCourse.getPrivilegeStrategys();
					if(psList.isEmpty()){
						tr6.addElement("td").addElement("strong").setText("无");
					}else{
						for(Iterator<PrivilegeStrategy> psIt = psList.iterator();psIt.hasNext();){
							PrivilegeStrategy ps = psIt.next();
							Element input = tr6.addElement("td").addElement("input");
							input.addAttribute("type","radio").addAttribute("name","privilegeStrategy").addAttribute("value",ps.getId()+"").setText(ps.getDescription());
						}
					}
				}else{
					resultRoot.addElement("tr").addElement("th").addAttribute("style","font-size: 13").setText("此课程信息未注册");
				}
				EntityManagerHelper.closeEntityManager();
			}
		}
		try {
			TransformerFactory.newInstance().newTransformer().transform(new DocumentSource(result),tResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
